package com.pda.common.basemvp.model;

import android.content.Context;
import com.google.gson.Gson;
import com.pda.common.utils.HttpConfig;
import com.pda.common.utils.PDAUtil;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

public abstract class BaseModel {
    protected String TAG = "BaseModel";
    protected OkHttpClient client;
    protected Gson gson;
    protected Context mContext;
    protected Retrofit retrofit;//异步
    protected HttpConfig httpConfig;
    protected Retrofit retrofitsync;//同步;
    protected HttpConfig httpConfigsync;
    protected final int connectTimeout = 15;
    protected final int readTimeout = 15;
    protected final int writeTimeout = 15;

    public BaseModel(Context context)
    {
        mContext = context;
        gson= new Gson();
        client = new OkHttpClient.Builder()
                .connectTimeout(connectTimeout, TimeUnit.SECONDS)
                .readTimeout(readTimeout, TimeUnit.SECONDS)
                .writeTimeout(writeTimeout, TimeUnit.SECONDS)
                .followRedirects(false)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(PDAUtil.hostname)//配置网络地址
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//配置回调库，采用RxJava
                //.addConverterFactory(GsonConverterFactory.create())//配置转化库，采用Gson
                .client(client)//配置连接框架okhttp
                .build();
        httpConfig = retrofit.create(HttpConfig.class);

        retrofitsync = new Retrofit.Builder()
                .baseUrl(PDAUtil.hostname)
                .client(client)
                .build();
        httpConfigsync = retrofitsync.create(HttpConfig.class);
    }

//    protected void uiThread()
//    {
//        mContext.runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                //此时已在主线程中，更新UI
//            }
//        });
//
//    }
}
